package org.locker.device.controller.admin;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.domain.vo.DeviceOperateLogVo;
import org.locker.domain.bo.DeviceOperateLogBo;
import org.locker.service.IDeviceOperateLogService;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 用户操作记录
 *
 * @author winnie
 * @date 2023-10-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/deviceOperateLog")
public class DeviceOperateLogController extends BaseController {

    private final IDeviceOperateLogService deviceOperateLogService;

    /**
     * 查询用户操作记录列表
     */
    @SaCheckPermission("device:deviceOperateLog:list")
    @GetMapping("/list")
    public TableDataInfo<DeviceOperateLogVo> list(DeviceOperateLogBo bo, PageQuery pageQuery) {
        return deviceOperateLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户操作记录列表
     */
    @SaCheckPermission("device:deviceOperateLog:export")
    @Log(title = "用户操作记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DeviceOperateLogBo bo, HttpServletResponse response) {
        List<DeviceOperateLogVo> list = deviceOperateLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户操作记录", DeviceOperateLogVo.class, response);
    }

    /**
     * 获取用户操作记录详细信息
     *
     * @param userOperateId 主键
     */
    @SaCheckPermission("device:deviceOperateLog:query")
    @GetMapping("/{userOperateId}")
    public R<DeviceOperateLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userOperateId) {
        return R.ok(deviceOperateLogService.queryById(userOperateId));
    }

    /**
     * 新增用户操作记录
     */
    @SaCheckPermission("device:deviceOperateLog:add")
    @Log(title = "用户操作记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DeviceOperateLogBo bo) {
        return toAjax(deviceOperateLogService.insertByBo(bo));
    }

    /**
     * 修改用户操作记录
     */
    @SaCheckPermission("device:deviceOperateLog:edit")
    @Log(title = "用户操作记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceOperateLogBo bo) {
        return toAjax(deviceOperateLogService.updateByBo(bo));
    }

    /**
     * 删除用户操作记录
     *
     * @param userOperateIds 主键串
     */
    @SaCheckPermission("device:deviceOperateLog:remove")
    @Log(title = "用户操作记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userOperateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userOperateIds) {
        return toAjax(deviceOperateLogService.deleteWithValidByIds(List.of(userOperateIds), true));
    }
}
